package com.jr.controller;

import com.google.gson.Gson;
import com.jr.pojo.Enterprise;
import com.jr.service.EnterpriseService;
import com.jr.service.impl.IEnterpriseServiceImpl;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

@WebServlet("/subinfo/*")
//subinfo/subinfo/queryInfo
public class InfomationController extends HttpServlet {
    EnterpriseService eps = new IEnterpriseServiceImpl();

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // super.service(req, resp); //只根据【请求方式】分发；但是现在需要根据【请求路径】分发
        String requestURI = request.getRequestURI();
        int index = requestURI.lastIndexOf("/") + 1;
        String mName = requestURI.substring(index);
        Class cla = this.getClass();
        Method method = null;
        try {
            method = cla.getMethod(mName, HttpServletRequest.class, HttpServletResponse.class);
            response.setContentType("application/json;charset=utf-8");
            request.setCharacterEncoding("utf-8");
            method.invoke(this, request, response);

        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (
                InvocationTargetException e) {
            e.printStackTrace();
        }
        System.out.println("我是servicecontroller" + "sssssssssssssssssssssssssssssss");
    }

    public void queryInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //subinfo/queryInfo
        /*判断输入的企业名是否在数据库中 如果存在进行回显数据 不存在就输入 调用加方法 存入数据库*/
        String edirname = request.getParameter("edirname");
        //System.out.println("用来判断企业名字是否存在在企业表中的名字判断" + edirname);//拿到了制造企业

        Enterprise ebter = new Enterprise();
        ebter.setEntername(edirname);
       // System.out.println("controller控制层 看看拿没拿到是制造业 企业名称2：：：：：" + ebter.getEntername());
        boolean b = eps.queryEnterprise(ebter);
        //System.out.println(b + "java说名称是否在企业表中");
        response.getWriter().println(new Gson().toJson(b));

    }

    public void addInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /*输入的企业名称不存在 随着页面输入数据  把输入的信息 传入到 数据库中*/
        System.out.println("a");
        System.out.println("我到addInfo这个区域了吗？");
        String edirname = request.getParameter("edirname");
        String uniformcode = request.getParameter("uniformcode");
        String scale = request.getParameter("scale");
        String email = request.getParameter("email");
        String phone = request.getParameter("phone");
        String fax = request.getParameter("fax");
        String address = request.getParameter("address");

        Enterprise enter = new Enterprise();
        enter.setEntername(edirname);
        enter.setUniformcode(uniformcode);
        enter.setScale(scale);
        enter.setEmail(email);
        enter.setPhone(phone);
        enter.setFax(fax);
        enter.setAddress(address);
        System.out.println("我是enter+++++++" + enter);
        System.out.println(enter.getPhone()+"拿到电话号码没有");
        boolean b = eps.addEnterprise(enter);
        System.out.println("我成功插入数据了吗？" + b);
        response.getWriter().println(new Gson().toJson(b));
    }

    public void queryEnterByName(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //Enterprise enter = new Enterprise();
        String edirname = request.getParameter("edirname");
        Enterprise enter = eps.queryENterByName(edirname);
       // System.out.println("企业规模是+++++++++++++++++++++++"+enter.getScale());
      response.getWriter().println(new Gson().toJson(enter));
        /*根据传过来的名字搜索出 名字符合的企业的其他信息*/
        /* eps.queryEnterprise(enter.getName());*/
    }

}
